package com.czhoujunlv.drug;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.czhoujunlv.drug.Bean.DataEntity;
import com.czhoujunlv.drug.Bean.Person_Info;
import com.czhoujunlv.drug.city.ScrollerNumberPicker;
import com.czhoujunlv.drug.fragment.HeadFragment;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.view.Gravity.LEFT;

public class Info extends AppCompatActivity {

    Person_Info person_info ;
    ImageView head,back;
    TextView name,id,pass,sex,age,address,tel;
    ConstraintLayout save;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        init();//初始化
        Thread thread = new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    String s = postInfo("http://192.168.43.138:8080/index/findConById/1","json");
                    Gson gson = new Gson();
                    person_info = new Person_Info();
                    person_info = gson.fromJson(s,Person_Info.class);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(person_info.getData() != null) {
                    Info.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(Info.this).load(person_info.getData().getImg()).into(head);
                            name.setText(person_info.getData().getName());
                            pass.setText("******");
                            sex.setText(person_info.getData().getSex());
                            age.setText(String.valueOf(person_info.getData().getAge()));
                            address.setText(person_info.getData().getAddress());
                            tel.setText(person_info.getData().getTelephone());
                        }
                    });
                }
            }
        };
        thread.start();

        name.setOnClickListener(new View.OnClickListener() {//修改name
            @Override
            public void onClick(View v) {
                TextView title = new TextView(Info.this);
                title.setText("请输入新的昵称");
                title.setTextSize(16);
                title.setGravity(Gravity.CENTER);
                title.setTextColor(Color.rgb(255,255,255));
                title.setHeight(80);
                title.setBackgroundColor(Color.rgb(18,144,220));
                final EditText editText = new EditText(Info.this);
                new AlertDialog.Builder(Info.this)
                        .setCustomTitle(title)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(editText)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        person_info.getData().name = editText.getText().toString();
                                        name.setText(editText.getText());
                                        save.setVisibility(View.VISIBLE);
                                    }
                                })
                        .setNegativeButton("取消", null).show();
            }
        });
        pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView title = new TextView(Info.this);
                title.setText("请输入新的密码");
                title.setTextSize(16);
                title.setGravity(Gravity.CENTER);
                title.setTextColor(Color.rgb(255,255,255));
                title.setHeight(80);
                title.setBackgroundColor(Color.rgb(18,144,220));
                LayoutInflater factory = LayoutInflater.from(Info.this);
                View textEntryView = factory.inflate(R.layout.passdialog, null);
                final EditText editText1 = textEntryView.findViewById(R.id.editext);
                final EditText editText2 = textEntryView.findViewById(R.id.editext2);
                final EditText editText3 = textEntryView.findViewById(R.id.editext1);
                new AlertDialog.Builder(Info.this)
                        .setCustomTitle(title)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(textEntryView)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        //person_info.getData().name = editText.getText().toString();
                                        Log.i("新密码：",editText2.getText().toString());
                                        if(!(editText1.getText().toString()).equals("123456")){
                                            Toast.makeText(Info.this,"原密码错误！！！",Toast.LENGTH_SHORT).show();
                                        }else{
                                            if(!editText2.getText().equals(editText3.getText())){
                                                Toast.makeText(Info.this,"两次输入的新密码不一致！！！",Toast.LENGTH_SHORT).show();
                                            }else{
                                                if (editText2.getText() == null) {
                                                    Toast.makeText(Info.this,"新密码不能为空！！！",Toast.LENGTH_SHORT).show();
                                                }else{
                                                    pass.setText(editText2.getText());
                                                    person_info.getData().pass = editText2.getText().toString();
                                                    Log.i("新密码：",editText2.getText().toString());
                                                    save.setVisibility(View.VISIBLE);
                                                }
                                            }
                                        }
                                    }
                                })
                        .setNegativeButton("取消", null).show();
            }
        });


        sex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] sexArray = new String[] { "男", "女" ,"保密"};
                TextView title = new TextView(Info.this);
                title.setText("选择性别");
                title.setTextSize(16);
                title.setGravity(Gravity.CENTER);
                title.setTextColor(Color.rgb(255,255,255));
                title.setHeight(80);
                title.setBackgroundColor(Color.rgb(18,144,220));
                new AlertDialog.Builder(Info.this)
                        .setCustomTitle(title)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setSingleChoiceItems(sexArray,3,new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                sex.setText(sexArray[which]);
                                person_info.getData().sex = sexArray[which];
                                save.setVisibility(View.VISIBLE);
                                dialog.dismiss();
                            }
                        }).setNegativeButton("取消", null).show();
            }
        });
        final EditText editText1 = new EditText(Info.this);
        age.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView title = new TextView(Info.this);
                title.setText("请输入新的年龄");
                title.setTextSize(16);
                title.setGravity(Gravity.CENTER);
                title.setTextColor(Color.rgb(255,255,255));
                title.setHeight(80);
                title.setBackgroundColor(Color.rgb(18,144,220));
                new AlertDialog.Builder(Info.this)
                        .setCustomTitle(title)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(editText1)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        person_info.getData().age = Integer.valueOf(editText1.getText().toString());
                                        age.setText(editText1.getText());
                                        save.setVisibility(View.VISIBLE);
                                    }
                                })
                        .setNegativeButton("取消", null).show();
            }
        });
        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(Info.this);
                View view = LayoutInflater.from(Info.this).inflate(R.layout.addressdialog, null);
                builder.setView(view);
                LinearLayout addressdialog_linearlayout = (LinearLayout)view.findViewById(R.id.addressdialog_linearlayout);
                final ScrollerNumberPicker provincePicker = (ScrollerNumberPicker)view.findViewById(R.id.province);
                final ScrollerNumberPicker cityPicker = (ScrollerNumberPicker)view.findViewById(R.id.city);
                final ScrollerNumberPicker counyPicker = (ScrollerNumberPicker)view.findViewById(R.id.couny);
                final AlertDialog dialog = builder.show();
                addressdialog_linearlayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        address.setText(provincePicker.getSelectedText()+cityPicker.getSelectedText()+counyPicker.getSelectedText());
                        save.setVisibility(View.VISIBLE);
                        Log.i("kkkk",provincePicker.getSelectedText()+cityPicker.getSelectedText()+counyPicker.getSelectedText());
                        dialog.dismiss();

                    }
                });
            }
        });
        save.setOnClickListener(new View.OnClickListener() {//保存，
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                final String obj2 = gson.toJson(person_info.getData());
                Log.i("json的obj2:",obj2);
                Thread thread1 = new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            postInfo("http://192.168.43.138:8080/index/consumer/updateConsumer",obj2);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                };
                thread1.start();
                EventBus.getDefault().post(name.getText().toString());
                finish();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(name.getText().toString());
                finish();
            }
        });
    }

    private void init() {
        back = findViewById(R.id.imageView17);
        head = findViewById(R.id.head);
        name = findViewById(R.id.name);
        pass = findViewById(R.id.pass);
        sex = findViewById(R.id.sex);
        age = findViewById(R.id.age);
        address = findViewById(R.id.address);
        tel = findViewById(R.id.tel);
        save = findViewById(R.id.save);
    }

    private String postInfo(String url,String s) throws IOException {
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),s);
        Request request = new Request.Builder()
                .url(url).post(body).build();
        Response response = HeadFragment.client.newCall(request).execute();
        if (response.isSuccessful()) {
            String res = response.body().string();
            Log.i("个人信息", res);
            return res;
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }
}